<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="75px"
    >
      <el-form-item label="科室名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入科室名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="昵称" prop="nickname">-->
<!--        <el-input-->
<!--          v-model="queryParams.nickname"-->
<!--          placeholder="请输入昵称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="电话" prop="phone">-->
<!--        <el-input-->
<!--          v-model="queryParams.phone"-->
<!--          placeholder="请输入电话"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="机构名称" prop="orgName">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgName"-->
<!--          placeholder="请输入机构名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="HIS编码" prop="hisDeptCode">
        <el-input
          v-model="queryParams.hisDeptCode"
          placeholder="请输入HIS科室编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="his科室名称" prop="hisDeptName">-->
<!--        <el-input-->
<!--          v-model="queryParams.hisDeptName"-->
<!--          placeholder="请输入his科室名称"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->


<!--      <el-form-item label="发布时间" prop="publishTime">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.publishTime"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-220px"-->
<!--        />-->
<!--      </el-form-item>-->


      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cms:department:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          @click="handleExport"-->
<!--          :loading="exportLoading"-->
<!--          v-hasPermi="['cms:department:export']"-->
<!--        >-->
<!--          <Icon icon="ep:download" class="mr-5px" /> 导出-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" border :stripe="true" :show-overflow-tooltip="true">
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="科室名称" align="center" prop="name" width="140" fixed="left"/>
<!--      <el-table-column label="昵称" align="center" prop="nickname" />-->
<!--      <el-table-column label="封面图" align="center" prop="coverImg" />-->
      <el-table-column label="电话" align="center" prop="phone"  width="160" />
      <el-table-column label="工作时段" align="center" prop="workRange"  width="150" />
<!--      <el-table-column label="擅长病种" align="center" prop="tags" />-->
      <el-table-column label="地址" align="center" prop="address"  width="160" />
<!--      <el-table-column label="机构名称" align="center" prop="orgName"  width="160" />-->
      <el-table-column label="HIS科室编码" align="center" prop="hisDeptCode"  width="100" />
      <el-table-column label="HIS科室名称" align="center" prop="hisDeptName"  width="100" />
<!--      <el-table-column label="链接地址" align="center" prop="linkUrl" />-->
<!--      <el-table-column label="提示" align="center" prop="tips" />-->
<!--      <el-table-column label="通知" align="center" prop="notice" />-->
<!--      <el-table-column label="描述" align="center" prop="description" />-->
<!--      <el-table-column label="内容" align="center" prop="content" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="图集" align="center" prop="pictures" />-->
<!--      <el-table-column label="附件" align="center" prop="files" />-->
      <el-table-column label="点击量" align="center" prop="views"  width="65" />
      <el-table-column label="顺序" align="center" prop="sort"  width="65"/>
      <el-table-column label="置顶状态" align="center" prop="topping" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.topping" />
        </template>
      </el-table-column>
      <el-table-column label="推荐状态" align="center" prop="recommend" width="80">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.recommend" />
        </template>
      </el-table-column>

      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="160"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="160"
      />
      <el-table-column label="有效状态" align="center" prop="publish" width="80" fixed="right">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="发布状态" align="center" prop="topping" width="80" fixed="right">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.publish" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="140px">
        <template #default="scope">
          <el-button
            plain
            size="small"
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:department:update']"
          >
            编辑
          </el-button>
          <el-button
            plain
            size="small"
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms:department:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DepartmentForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE, getStrDictOptions, getDictOptions, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import { DepartmentApi, DepartmentVO } from '@/api/cms/department'
import DepartmentForm from './DepartmentForm.vue'

/** 科室信息 列表 */
defineOptions({ name: 'Department' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DepartmentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  nickname: undefined,
  coverImg: undefined,
  phone: undefined,
  workRange: undefined,
  tags: undefined,
  address: undefined,
  orgName: undefined,
  hisDeptCode: undefined,
  hisDeptName: undefined,
  linkUrl: undefined,
  tips: undefined,
  notice: undefined,
  description: undefined,
  content: undefined,
  contentFormat: undefined,
  remark: undefined,
  pictures: undefined,
  files: undefined,
  views: undefined,
  topping: undefined,
  recommend: undefined,
  publishTime: [],
  publish: undefined,
  sort: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DepartmentApi.getDepartmentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DepartmentApi.deleteDepartment(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DepartmentApi.exportDepartment(queryParams)
    download.excel(data, '科室信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
/*:deep(.el-table) {*/
/*position: relative;*/
/*overflow: hidden;*/
/*-webkit-box-sizing: border-box;*/
/*box-sizing: border-box;*/
/*-webkit-box-flex: 1;*/
/*-ms-flex: 1;*/
/*flex: 1;*/
/*width: 100%;*/
/*max-width: 100%;*/
/*font-size: 12px;*/
/*color: #000000;*/
/*}*/
/*:deep(.REFUND-row) {*/
/*background-color: #F3D39BFF !important;*/
/*}*/

/*:deep(.fails-row) {*/
/*background-color: #D15B47 !important;*/
/*}*/

/*:deep(.success-row) {*/
/*background-color: #36bc99 !important;*/
/*}*/
/*:deep(.my-table td) {*/
/*padding: 5px 0 !important;*/
/*}*/

:deep(.el-table--default .cell) {
  padding: 0 6px !important;
}

</style>
